package ui.home.column2.channel_mapping

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import model.enums.ChannelMappingTabSelect
import model.enums.ContentType
import model.vo.FriendsMappingItem
import model.vo.GroupMappingItem
import theme.EasyChatTheme
import ui.home.HomeViewModel
import ui.home.column2.channel_mapping.friends.FriendsMappingList
import ui.home.column2.channel_mapping.group.GroupMappingList


/**
 * @author wei kunJie
 * @date 2024/3/25 14:41:53
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ChannelMapping(modifier: Modifier,
                   tabSelect: MutableState<ChannelMappingTabSelect>,
                   friendsMappingItems: MutableList<FriendsMappingItem>,
                   groupMappingItems: MutableList<GroupMappingItem>,
                   column3ContentType:MutableState<ContentType>,
                   homeViewModel: HomeViewModel,
                   easyChatTheme: MutableState<EasyChatTheme>,
) {
    Column(modifier = modifier) {
        //好友通知
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .combinedClickable(onClick = {
                    column3ContentType.value=ContentType.FriendNotification
                })
        ) {
            Text(
                text = "好友通知",
                modifier = modifier.weight(1f).padding(start = 16.dp, top = 11.dp, bottom = 11.dp),
                style = TextStyle(
                    textAlign = TextAlign.Start,
                    fontSize = 15.sp,
                    color = Color.Black
                )
            )
            Icon(
                painter = painterResource("icon/ic_right.svg"),
                contentDescription = "右",
                tint = Color.Black,
                modifier = Modifier.size(20.dp).padding(3.dp)
            )
            Spacer(Modifier.width(14.dp))
        }
        //群通知
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .combinedClickable(onClick = {
                    column3ContentType.value=ContentType.GroupNotification
                })
        ) {
            Text(
                text = "群通知", modifier = modifier.weight(1f).padding(start = 16.dp, top = 11.dp, bottom = 11.dp),
                style = TextStyle(
                    textAlign = TextAlign.Start,
                    fontSize = 15.sp,
                    color = Color.Black
                )
            )
            Icon(
                painter = painterResource("icon/ic_right.svg"),
                contentDescription = "右",
                tint = Color.Black,
                modifier = Modifier.size(20.dp).padding(3.dp)
            )
            Spacer(Modifier.width(14.dp))
        }
        //分割线（横）
        Spacer(
            modifier = Modifier.fillMaxWidth().height(1.dp)
                .background(color = Color(0x22747A80))
        )
        //好友与群聊Tab选择菜单
        Row(
            modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp)
                .background(color = Color(0X11363636), shape = RoundedCornerShape(4.dp))
        ) {
            Box(
                modifier = Modifier.weight(1f).height(35.dp).padding(5.dp)
                    .background(
                        color = if (tabSelect.value == ChannelMappingTabSelect.Friends) easyChatTheme.value.mainWhite
                        else Color.Transparent,
                        shape = RoundedCornerShape(4.dp)
                    ).customCombinedClickable {
                        tabSelect.value = ChannelMappingTabSelect.Friends
                    },
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "好友",
                    style = TextStyle(
                        textAlign = TextAlign.Center,
                        fontSize = 14.sp,
                        color = if (tabSelect.value == ChannelMappingTabSelect.Friends) easyChatTheme.value.mainColor
                        else Color.Gray
                    ),

                    )
            }
            Spacer(Modifier.width(5.dp))
            Box(modifier = Modifier.weight(1f).height(35.dp).padding(5.dp).background(
                color = if (tabSelect.value == ChannelMappingTabSelect.Group) easyChatTheme.value.mainWhite
                else Color.Transparent,
                shape = RoundedCornerShape(4.dp)
            ).customCombinedClickable {
                tabSelect.value = ChannelMappingTabSelect.Group
            }, contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "群聊",
                    style = TextStyle(
                        textAlign = TextAlign.Center,
                        fontSize = 14.sp,
                        color = if (tabSelect.value == ChannelMappingTabSelect.Group) easyChatTheme.value.mainColor
                        else Color.Gray
                    ),

                    )
            }
        }
        if (tabSelect.value == ChannelMappingTabSelect.Friends) {
            //好友列表
            FriendsMappingList(easyChatTheme,Modifier,friendsMappingItems,homeViewModel)
        } else if (tabSelect.value == ChannelMappingTabSelect.Group) {
            //群聊列表
            GroupMappingList(easyChatTheme,Modifier,groupMappingItems,homeViewModel)
        }

    }
}

@ExperimentalFoundationApi
fun Modifier.customCombinedClickable(
    enabled: Boolean = true,
    onClickLabel: String? = null,
    role: Role? = null,
    onLongClickLabel: String? = null,
    onLongClick: (() -> Unit)? = null,
    onDoubleClick: (() -> Unit)? = null,
    onClick: () -> Unit
) = composed(
    inspectorInfo = debugInspectorInfo {
        name = "combinedClickable"
        properties["enabled"] = enabled
        properties["onClickLabel"] = onClickLabel
        properties["role"] = role
        properties["onClick"] = onClick
        properties["onDoubleClick"] = onDoubleClick
        properties["onLongClick"] = onLongClick
        properties["onLongClickLabel"] = onLongClickLabel
    }
) {
    Modifier.combinedClickable(
        enabled = enabled,
        onClickLabel = onClickLabel,
        onLongClickLabel = onLongClickLabel,
        onLongClick = onLongClick,
        onDoubleClick = onDoubleClick,
        onClick = onClick,
        role = role,
        indication = null,
        interactionSource = remember { MutableInteractionSource() }
    )
}